const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const axios = require('axios');
const newsPath = "/news"

exports.createPages = async ({ graphql, actions }) => {
	const { createPage, createRedirect } = actions
	// 主页
	createPage({
		path: '/',
		component: path.resolve("./src/templates/Home/index.js"),
	})
	// // 活动页
	// createPage({
	// 	path: '/activity',
	// 	component: path.resolve("./src/templates/Activity/index.js"),
	// })
	// 新闻页面
	createPage({
		path: `/news`,
		component: path.resolve("./src/templates/BlogList/index.js"),
		context: {
			limit: 10,
			skip: 1,
			numPages: 0,
			currentPage: 0,
			filter: {
				type: { eq: "vul" },
			},
		},
	})
	createPage({
		path: `/news/vul`,
		component: path.resolve("./src/templates/BlogList/index.js"),
		context: {
			limit: 10,
			skip: 1,
			numPages: 0,
			currentPage: 0,
			filter: {
				type: { eq: "vul" },
			},
		},
	})
	// 服务公告
	createPage({
		path: `news/service`,
		component: path.resolve("./src/templates/BlogList/index.js"),
		context: {
			limit: 10,
			skip: 1,
			numPages: 0,
			currentPage: 0,
			filter: {
				type: { eq: "service" },
			},
		},
	})
	// 文章
	createPage({
		path: `news/product`,
		component: path.resolve("./src/templates/BlogList/index.js"),
		context: {
			filter: {
				type: { eq: "product" },
			},
		},
	})
	//详细页
	createPage({
		path:  `/news/detail` ,
		component: path.resolve("./src/templates/BlogList/index.js"),
		context: {
			filter: {
				type: { eq: "detail" },
			},
		},
	})
	createPage({
		path:  `/news/product/detail` ,
		component: path.resolve("./src/templates/BlogList/index.js"),
		context: {
			filter: {
				type: { eq: "product" },
				detail: { eq: "detail" },
			},
		},
	})
	createPage({
		path:  `/news/vul/detail` ,
		component: path.resolve("./src/templates/BlogList/index.js"),
		context: {
			filter: {
				type: { eq: "vul" },
				detail: { eq: "detail" },
			},
		},
	})
	createPage({
		path:  `/news/service/detail` ,
		component: path.resolve("./src/templates/BlogList/index.js"),
		context: {
			filter: {
				type: { eq: "service"},
				detail: { eq: "detail"},
			},
		},
	})
}
exports.createSchemaCustomization = ({ actions }) => {
	const { createTypes } = actions
	const typeDefs = `
    type mongodbCloudmonitorNews implements Node @dontInfer {
      title: String!
      author: String
      type: String!
			link: String
			summary: String
			content: String
			isSendNotification: Boolean
			isConsoleDisplay: Boolean
			isHomepageDisplay: Boolean
			isRecommendDisplay: Boolean
			isDraft: Boolean
			datetime: Date @dateformat(formatString: "YYYY-MM-DD")
			mongodb_id: String!
    }
  `
	createTypes(typeDefs)
}

exports.onCreateNode = ({ node, actions, getNode }) => {
	const { createNodeField } = actions

	if (node.internal.type === `MongodbCloudmonitorNews`) {
		const value = createFilePath({ node, getNode })
		createNodeField({
			name: `id`,
			node,
			value,
		})
	}
}
exports.onCreateWebpackConfig = ({ actions, stage }) => {
  if (stage === 'build-javascript') {
    // turn off source-maps
    actions.setWebpackConfig({
      devtool: 'source-map'
    })
  }
};
